#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 31;
pair<int, int> node[N];
int mid[N], pre[N];
int dfs(int l1, int r1, int l2, int r2)
{
	if (l1 > r1 || l2 > r2)
		return -1;
	int rt = pre[l2];
	int p = l1;
	while (mid[p] != rt)
		p++;

	int cnt = p - l1;

	node[rt].first = dfs(p + 1, r1, l2 + cnt + 1, r2);
	node[rt].second = dfs(l1, p - 1, l2 + 1, l2 + cnt);
	return rt;

}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> mid[i];
	for (int i = 0; i < n; i++)
		cin >> pre[i];

	int ret = dfs(0, n - 1, 0, n - 1);
	queue<int> q;
	q.push(ret);
	vector<int> v;
	while (q.size())
	{
		int t = q.front();
		q.pop();
		v.push_back(t);
		if (node[t].first > 0)
			q.push(node[t].first);
		if (node[t].second > 0)
			q.push(node[t].second);
	}
	for (int i = 0; i < v.size(); i++)
	{
		if (i != 0)
			cout << ' ';
		cout << v[i];
	}
	return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;

struct node {
	int id = N;
	int cnt;
	double house_total;
	double house_area;
}a[N];
int f[N];
int vis[N];
int Find(int x)
{
	if (f[x] != x)
	{
		f[x] = Find(f[x]);
	}
	return f[x];
}
void Union(int x, int y)
{
	x = Find(x), y = Find(y);
	if (x < y)
	{
		f[y] = x;
	}
	else {
		f[x] = y;
	}
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < N; i++)
		f[i] = i;
	while (n--)
	{
		int id, f, m, k;
		cin >> id >> f >> m >> k;
		vis[id] = 1;
		if (f != -1)
		{
			Union(id, f);
			vis[f] = 1;
		}
		if (m != -1)
		{
			Union(id, m);
			vis[m] = 1;
		}
		for (int i = 0; i < k; i++)
		{
			int son;
			cin >> son;
			Union(id, son);
			vis[son] = 1;
		}
		cin >> a[id].house_total >> a[id].house_area;
	}
	for (int i = 0; i < N; i++)
	{
		if (vis[i])
		{
			int u = Find(i);
			a[u].cnt++;
			a[u].id = min(a[u].id, i);
			if (u == i)
				continue;
			a[u].house_total += a[i].house_total;
			a[u].house_area += a[i].house_area;
		}
	}
	int ret = 0;
	for (int i = 0; i < N; i++)
	{
		if (vis[i] && i == Find(i))
		{
			a[i].house_total /= a[i].cnt;
			a[i].house_area /= a[i].cnt;
			ret++;
		}
	}
	sort(a, a + N, [](const node& p1, const node& p2) {
		if (p1.house_area == p2.house_area)
			return p1.id < p2.id;
		return p1.house_area > p2.house_area;
		});
	cout << ret << endl;
	for (int i = 0; i < N; i++)
	{
		if (a[i].cnt > 0)
		{
			printf("%04d %d %.3lf %.3lf\n", a[i].id, a[i].cnt, a[i].house_total, a[i].house_area);
		}
	}

	return 0;
}